package com.customization.shunsheng;

import cn.hutool.core.date.DateUtil;
import com.engine.core.cfg.annotation.CommandDynamicProxy;
import com.engine.core.interceptor.AbstractCommandProxy;
import com.engine.core.interceptor.Command;
import com.engine.kq.cmd.leavetypes.AddLeaveTypesCmd;
import org.apache.commons.lang.StringUtils;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.User;

import java.util.*;

/**
 * 新建假期类型，将假期类型为外籍类型的员工字段保存到假期类型建模表
 */
@CommandDynamicProxy(target = AddLeaveTypesCmd.class )
public class AddLeaveTypesCmdIntercept extends AbstractCommandProxy<Map<String,Object>> {

    BaseBean basebean = new BaseBean();

    @Override
    public Map<String, Object> execute(Command<Map<String, Object>> command) {
        AddLeaveTypesCmd cmd = (AddLeaveTypesCmd) command;
        Map<String,Object> params=cmd.getParams();
        Map<String, Object> result = nextExecute(cmd);

        RecordSet rs = new RecordSet();
        RecordSet rs1 = new RecordSet();
        User user = cmd.getUser();
        Integer foreignLeave = Util.getIntValue(Util.null2String(params.get("foreignLeave")));
        String leaveName = Util.null2String(params.get("leaveName"));
        String hrmResourceIds = Util.null2String(params.get("hrmResourceId"));
        Integer id = -1;
        if ( foreignLeave == 1 && StringUtils.isNotBlank(leaveName) && StringUtils.isNotBlank(hrmResourceIds)) {

            String[] idsArr = hrmResourceIds.split(",");
            String getLeaveIdSql = "select id from kq_LeaveRules where leaveName = ? ";
            rs.executeQuery(getLeaveIdSql,leaveName);
            while (rs.next()) {
                id = Util.getIntValue(Util.null2String(rs.getString("id")));
            }
            if ( id > -1) {
                //将该外籍假期类型对应的假期类型记录
                String leaveType = Util.null2String(params.get("leaveType"));
                String moSql = "update kq_LeaveRules set corresLeaveType = ?,isForg = 1 where id = ?";
                rs.executeUpdate(moSql,leaveType,id);

                List< Map < String, Object>> resList = new ArrayList<>();
                String getResInfos = "select workcode, subcompanyid1, departmentid from hrmresource where id = ?";
                for ( String res :idsArr) {
                    rs.executeQuery(getResInfos,res);
                    while (rs.next()) {
                        String workcode = Util.null2String(rs.getString("workcode"));
                        String departmentid = Util.null2String(rs.getString("departmentid"));
                        String subcompany = Util.null2String(rs.getString("subcompanyid1"));
                        if ( StringUtils.isNotBlank(workcode) && StringUtils.isNotBlank(departmentid) && StringUtils.isNotBlank(subcompany)) {
                            Map < String, Object> resMap = new HashMap<>();
                            resMap.put("resourceId",res);
                            resMap.put("workcode",workcode);
                            resMap.put("departmentid",departmentid);
                            resMap.put("subcompany",subcompany);
                            resList.add(resMap);
                        }
                    }
                }

                if ( !resList.isEmpty()) {
                    //获取假期类型建模表模块id
                    String leaveTypeModeId = Util.null2String(basebean.getPropValue("propExtends", "leaveTypeModeId"));
                    String nowDate = DateUtil.format(new Date(),"yyyy-MM-dd");
                    String nowTime = DateUtil.format(new Date(),"HH:mm:ss");

                    for ( Map< String, Object> resInfos: resList) {
                        String resourceId = Util.null2String(resInfos.get("resourceId"));
                        String workcode = Util.null2String(resInfos.get("workcode"));
                        String departmentid = Util.null2String(resInfos.get("departmentid"));
                        String subcompany = Util.null2String(resInfos.get("subcompany"));
                        String insertResSql = "insert into uf_jqlxb (ygxm,ygbh,jqlx,ygszfb,ygszbm,isdelete," +
                                "formmodeid,modedatacreater,modedatacreatertype,modedatacreatedate,modedatacreatetime) " +
                                "values (?,?,?,?,?,?,?,?,?,?,?)";
                        rs1.executeUpdate(insertResSql,resourceId,workcode,id,subcompany,departmentid,"1",
                                leaveTypeModeId,user.getUID(),user.getType(),nowDate,nowTime);
                    }
                }

            }

        } else if (foreignLeave == 0 || foreignLeave == -1) {
            String getLeaveIdSql = "select id from kq_LeaveRules where leaveName = ? ";
            rs.executeQuery(getLeaveIdSql,leaveName);
            while (rs.next()) {
                id = Util.getIntValue(Util.null2String(rs.getString("id")));
            }
            String moSql = "update kq_LeaveRules set isForg = 0 where id = ?";
            rs.executeUpdate(moSql,id);
        }

        return result;
    }
}
